<template>
  <!-- 行业人才需求 -->
  <div
    class="cardWrapper"
    :id="`menuScroll${scrollFirstIndex}-${componentsIndex}`"
    v-if="layerItem.children.length"
  >
    <div class="cardTitleContainer">
      <img
        src="https://img3.job1001.com/data_center/images/position_item_icon.png"
        alt="Icon"
        class="position_item_icon"
      />
      <div class="cardTitle">{{ layerItem.title }}</div>
    </div>
    <div class="layerList">
      <el-row
        class="demo-autocomplete"
        :key="index"
        v-for="(item, index) in layerItem.children || layerList"
      >
        <!-- <div class="demo-autocomplete-content" v-if="handleDisplayContent(item)"> -->
        <div class="demo-autocomplete-content">
          <div class="controlLine" :id="`scroll${componentsIndex}-${index}`">
            <span class="tinyTitle">
              <div class="titleFlag"></div>
              {{ item.title }}
            </span>
            <div class="line"></div>
          </div>
          <!-- 以上是标题，以下是内容，目前是只改变内容就好了 -->
          <template v-if="item.layerId == 186">
            <el-form ref="form" :model="form">
              <el-form-item label="统计时间：">
                <el-date-picker
                  v-model="form.selectBetweenTime"
                  type="monthrange"
                  :clearable="false"
                  size="small"
                  align="right"
                  unlink-panels
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始月份"
                  end-placeholder="结束月份"
                  :picker-options="pickerOptions"
                  @change="handleDateChange"
                >
                </el-date-picker>
              </el-form-item>
            </el-form>
            <div class="chartContainer">
              <WordCloud :wordData="industryWordData" :layerId="item.layerId" />
              <div
                class="chartDesc"
                v-if="industryWordData && industryWordData.length"
              >
                <div
                  class="chartDescItem"
                  v-for="(item, index) in industryWordData"
                  :key="index"
                >
                  <div class="chartDescTitle">
                    {{ item.name }}行业招聘岗位数量为
                  </div>
                  <div class="chartDescNum">{{ item.value }} 个</div>
                </div>
              </div>
            </div>
          </template>
          <template v-if="item.layerId == 187">
            <div class="chartContainer">
              <WordCloud :wordData="companyWordData" :layerId="item.layerId" />
              <div
                class="chartDesc"
                v-if="companyWordData && companyWordData.length"
              >
                <div
                  class="chartDescItem"
                  v-for="(item, index) in companyWordData"
                  :key="index"
                >
                  <div class="chartDescTitle">
                    {{ item.name }}规模公司招聘岗位数量为
                  </div>
                  <div class="chartDescNum">{{ item.value }} 个</div>
                </div>
              </div>
            </div>
          </template>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>
import { reqTalentRequirementWordCloud } from '@/api/chart.js';
import WordCloud from '@/components/charts/WordCloud.vue';
export default {
  name: 'talentDemand',
  components: {
    WordCloud
  },
  props: {
    layerList: {
      type: Array,
      default: () => []
    },
    layerItem: {
      type: Object,
      default: () => {}
    },
    topId: {
      type: String,
      required: true
    },
    isSuperToken: {
      type: Boolean,
      default: false
    },
    scrollFirstIndex: {
      type: [String, Number],
      default: 0
    },
    aiCreateStatus: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    layerItem: {
      handler(newValue, oldValue) {
        if (newValue && newValue.layerId) {
          this.layerList.forEach((item, index) => {
            if (item.layerId == newValue.layerId) {
              this.componentsIndex = index;
            }
          });
        }
      },
      immediate: true
    }
  },
  data() {
    return {
      industryWordData: [],
      companyWordData: [],
      currentEdit: false,
      positionId: this.$route.query.id,
      componentsIndex: '', //用来标记组件下标，滚动用
      layerDataList: [],
      messages: [],
      currentButton: '',
      form: {
        selectBetweenTime: []
      },
      pickerOptions: {
        shortcuts: [
          {
            text: '今年至今',
            onClick(picker) {
              const end = new Date();
              const start = new Date(new Date().getFullYear(), 0);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '最近六个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setMonth(start.getMonth() - 6);
              picker.$emit('pick', [start, end]);
            }
          }
        ]
      }
    };
  },
  created() {
    if (!this.form.selectBetweenTime.length) {
      // 获取当前日期
      const end = new Date();

      // 计算7个月前的日期
      const start = new Date();
      start.setMonth(start.getMonth() - 7);

      // 格式化日期为 yyyy-MM-dd 格式
      const startFormatted = [
        start.getFullYear(),
        String(start.getMonth() + 1).padStart(2, '0'),
        String(start.getDate()).padStart(2, '0')
      ].join('-');

      const endFormatted = [
        end.getFullYear(),
        String(end.getMonth() + 1).padStart(2, '0'),
        String(end.getDate()).padStart(2, '0')
      ].join('-');

      // 将格式化后的日期赋值给 selectBetweenTime
      this.form.selectBetweenTime = [startFormatted, endFormatted];
    }
    this.getWordCloadData(1);
    this.getWordCloadData(2);
  },
  methods: {
    handleDateChange(val) {
      this.getWordCloadData(1);
    },
    getWordCloadData(keyType) {
      const params = {
        ylPositionId: this.positionId,
        keyType: keyType, //1:国标行业 2:企业规模
        startTime: this.form.selectBetweenTime[0],
        endTime: this.form.selectBetweenTime[1],
        pageNo: '1',
        pageSize: '15'
      };
      // //console.log("getWordCloadData", params);

      reqTalentRequirementWordCloud(params)
        .then((res) => {
          if (res.code == 200) {
            const colors = [
              '#FF5733', // 鲜艳的红橙色
              '#FF8C00', // 橙色
              '#FFD700', // 金色
              '#32CD32', // 酸橙绿色
              '#00FA9A', // 中春绿色
              '#00BFFF', // 深天蓝色
              '#1E90FF', // 道奇蓝色
              '#4682B4', // 钢蓝色
              '#8A2BE2', // 蓝紫色
              '#DA70D6', // 中紫色
              '#DDA0DD', // 浅紫色
              '#FF1493', // 深粉色
              '#FF69B4', // 热粉色
              '#C0C0C0', // 银色
              '#A9A9A9', // 深灰色
              '#808080', // 标准灰色
              '#A52A2A', // 棕色
              '#8B4513', // 赛璐璐色
              '#B22222', // 烧红色
              '#DC143C' // 猩红色
            ];
            // //console.log("res.data", res.data);
            switch (keyType) {
              case 1:
                this.industryWordData = res.data.map((item, index) => {
                  return {
                    name: item.keyName,
                    value: item.positionCnt,
                    textStyle: {
                      color: colors[index]
                    }
                  };
                });
                break;
              case 2:
                this.companyWordData = res.data.map((item, index) => {
                  return {
                    name: item.keyName,
                    value: item.positionCnt,
                    textStyle: {
                      color: colors[index]
                    }
                  };
                });
                break;

              default:
                break;
            }
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    //处理一下用来显示的内容
    handleDisplayContent(item) {
      const data = item.data;
      if (data instanceof Array) {
        let content = '';

        data.forEach((_item, index) => {
          if (_item.description && _item.layerValue) {
            content +=
              index +
              1 +
              '. ' +
              _item.description +
              ' ' +
              _item.layerValue +
              '<br />';
          } else {
            content +=
              index +
              1 +
              '. ' +
              _item.tagName +
              ' ' +
              _item.tagTimeDesc +
              '<br />';
          }
        });
        return content;
      }

      if (data instanceof Object) {
        const content = data.answer ? data.answer.answerContent : '待补充';
        return content.replace(/\n/g, '<br>');
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.cardWrapper {
  // position: relative;
  min-width: 800px;
  border-radius: 10px;
  border: 1px solid #cbdcfe;
  background-size: 110% 219px;
  background-position: -10px -10px;
  background-image: url('https://img3.job1001.com/data_center/images/salary_bg.png');
  background-repeat: no-repeat;
  padding: 20px;
  flex: 1;
  margin: 0;

  margin-bottom: 14px;
  margin-top: 20px;
  &::-webkit-scrollbar {
    width: 4px;
    background-color: #fff;
    border-radius: 4px;

    height: 4px;
  }
  &::-webkit-scrollbar-thumb {
    background-color: #d8d8d8;
    border-radius: 4px;
  }
  .cardTitleContainer {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    // position: sticky;
    top: 0;
    .cardTitle {
      height: 30px;
      font-size: 22px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 600;
      color: #000000;
      line-height: 30px;
      margin-right: 15px;
    }
    .introduceJobInput {
      width: 400px;
    }
    .introduceJob {
      height: 21px;
      font-size: 15px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #999999;
      line-height: 21px;
      margin-right: 38px;
    }
    .position_item_icon {
      width: 39px;
      height: 40px;
      margin-right: 13px;
    }
  }

  .layerList {
    .demo-autocomplete {
      display: flex;
      .demo-autocomplete-content {
        flex: 1;
      }
      .positionPic {
        width: 140px;
        // height: 137px;
        border-radius: 16px;
        opacity: 0.77;
      }
    }
  }

  // 以下是通用样式
  .contentItemEditAndAIBtn {
    line-height: 32px;
    margin: 0 10px 0 15px;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #888888;
    display: flex;
    align-items: center;
    cursor: pointer;
    img {
      width: 22px;
      height: 22px;
    }
  }
  .titleFlag {
    width: 5px;
    height: 15px;
    border-radius: 3px;
    background: #4880ff;
    margin-right: 12px;
  }
  .controlLine {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin-bottom: 15px;
    .tinyTitle {
      display: flex;
      align-items: center;
      font-weight: bold;
      font-size: 18px;
      // margin-right: 24px;
    }
    .line {
      flex: 1;
      // height: 16px;
      margin-left: 14px;
      border-bottom: 1px solid #efefef;
    }
    .saveBtns {
      display: flex;
      height: 32px;
      align-items: center;
      justify-content: center;
      // padding-right: 10px;
      .contentItemEditAndAIBtn {
        line-height: 32px;
        margin: 0 10px 0 15px;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #888888;
        display: flex;
        align-items: center;
        cursor: pointer;
        img {
          width: 22px;
          height: 22px;
        }
      }
    }
    .AILoading {
      position: relative;
      &::after {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        width: 0px;
        height: 100%;
        background-color: transparent;
        animation-name: ellipsis;
        animation-duration: 1.5s;
        animation-iteration-count: infinite;
      }
    }
  }
  .editBox {
    width: 100%;
    display: flex;
    // padding-right: 20px;
    margin-bottom: 52px;

    .otherNameContainer {
      display: flex;
      flex-wrap: wrap;
      max-width: 50%;
      align-content: flex-start;
      flex: 1;
      .otherName {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 38px;
        border-radius: 4px;
        border: 1px solid #b2bad4;
        padding: 7px 18px;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #333333;
        line-height: 24px;
        margin-right: 18px;
        margin-bottom: 24px;
      }
    }
    .similerContainer {
      display: flex;
      flex-wrap: wrap;
      flex: 1;
      max-width: 50%;
      align-content: flex-start;
      .similer {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 38px;
        background: #edf4ff;
        border-radius: 6px;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #333333;
        line-height: 24px;
        margin-right: 18px;
        padding: 7px 18px;
        margin-bottom: 24px;
      }
    }
    .editBoxLeft {
      flex: 1;
      position: relative;
      border: 1px solid #709cff;
      background-color: white;
      border-radius: 6px 0px 0px 6px;

      .deleteContent {
        position: absolute;
        cursor: pointer;
        right: 12px;
        top: 15px;
      }
      .fakeTextArea {
        position: relative;
        background-color: white;
        min-height: 475px;
        padding: 14px;
        .listContentItem {
          // background-color: gray;
          position: relative;
          border-radius: 4px;
          margin-bottom: 29px;
          textarea {
            position: relative;
            background-color: #eeeeee;
            font-size: 15px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #555555;
            line-height: 26px;
            padding: 4px 5px 0px 6px;
            height: 100% !important;
          }
          .deleteListContent {
            position: absolute;
            display: flex;
            justify-content: flex-end;
            right: 0;
            width: 100%;
            background-color: #eeeeee;
            padding-right: 6px;
            padding-bottom: 8px;
            img {
              cursor: pointer;
            }
          }
        }
        .addListContentBtn {
          width: 100%;
          height: 32px;
          background: #ffffff;
          border-radius: 4px;
          border: 1px solid #4880ff;
          color: #4880ff;
          cursor: pointer;
        }
      }
    }
    .editBoxRight {
      flex: 1;
      padding: 0px 16px 0px 16px;
      background: #f5f9ff;
      font-size: 15px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #4b6aaf;
      line-height: 27px;
      position: relative;
      padding-bottom: 20px;
      border-radius: 0px 6px 6px 0px;
      .extendsItem {
        position: relative;
        padding-bottom: 35px;
        padding: 0px 16px;
        .replaceByOne {
          display: flex;
          justify-content: flex-end;
          position: relative;
          bottom: 0;
          right: 14px;
          cursor: pointer;
          color: #4880ff;
        }
      }
    }
    .noEdit {
      // padding: 0;
      background: transparent;
    }
    .playlistContainer {
      display: flex;
      // width: 251px;
      height: 160px;
      overflow-x: auto;
      overflow-y: hidden;
      margin-left: 47px;
      .playlistItem {
        position: relative;
        width: 217.64px;
        height: 137px;
        margin-right: 10px;
        cursor: pointer;
        &::after {
          content: '';
          position: absolute;
          bottom: 0;
          left: 0;
          width: 100%;
          height: 41px;
          background: linear-gradient(
            180deg,
            rgba(0, 0, 0, 0) 0%,
            rgba(0, 0, 0, 0.3) 100%
          );
          border-radius: 0px 0px 15px 15px;
          z-index: 1;
        }
        img {
          width: 217.64px;
          height: 137px;
          border-radius: 16px;
        }
        .playlistItemName {
          position: absolute;
          bottom: 11px;
          left: 0;
          padding: 0 13px;
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 16px;
          color: #fff;
          line-height: 22px;
          text-align: left;
          font-style: normal;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-inline-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 1;
          z-index: 2;
        }
      }
      &::-webkit-scrollbar-track-piece {
        background: #dee6ec;
      }

      &::-webkit-scrollbar {
        height: 6px;
      }

      &::-webkit-scrollbar-thumb {
        background: #bccad5;
        border-radius: 20px;
      }
    }
  }
  .aiContent {
    flex: 1;
    height: 100%;
    font-size: 15px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #555555;
    line-height: 27px;
    // max-width: 90%;
    word-break: break-all;
    white-space: normal;
  }
  .saveAllBtn {
    width: 57px;
    height: 26px;
    border: none;
    background: #4880ff;
    color: #ffffff;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    border-radius: 4px;
    margin-left: 15px;
    margin-right: 20px;
    cursor: pointer;
  }
}
.chartContainer {
  display: flex;
  .chartDesc {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    width: 40%;
    margin: 10px 10px 10px 10px;
    .chartDescItem {
      margin: 4px 0;
      width: 48%;
      .chartDescTitle {
        color: #555;
        font-size: 14px;
      }
      .chartDescNum {
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -webkit-box-align: baseline;
        -ms-flex-align: baseline;
        align-items: baseline;
        color: #1b78ff;
        font-size: 24px;
        font-weight: 700;
        margin-top: 4px;
        font-family: D-DIN-PRO-500-Medium;
      }
    }
  }
}
::v-deep .el-date-editor .el-range-input {
  background-color: #fff !important;
}
</style>
